import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import goods from '@/views/goods/index.vue'
import goodClass from '@/views/goodClass/index.vue'
import property from '@/views/property/index.vue'
import propertyValues from '@/views/propertyValues/index.vue'
import statistics from '@/views/statistics/index.vue'
import customer_orders from '@/views/customer_orders/index.vue'
import store from '@/views/store/index.vue'
import login from '@/views/login/index.vue'
import user from '@/views/user/index.vue'
import member from '@/views/member/index.vue'
import register from '@/views/register/index.vue'
import sotre from '@/views/sotre/index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: `/`,
    redirect: `/statistics`,
    name: `Home`,
    component: Home,
  },
  {
    path: `/statistics`,
    name: `statistics`,
    component: statistics,
  },
  {
    path: `/login`,
    name: `login`,
    component: login,
  },
  {
    path: `/register`,
    name: `register`,
    component: register,
  },
  {
    path: `/store`,
    name: `store`,
    component: store,
  },
  {
    path: `/user`,
    name: `user`,
    component: user,
  },
  {
    path: `/member`,
    name: `member`,
    component: member,
  },
  {
    path: `/sotre`,
    name: `sotre`,
    component: sotre,
  },
  {
    path: `/customer_orders`,
    name: `customer_orders`,
    component: customer_orders,
  },
  {
    path: `/propertyValues`,
    name: `propertyValues`,
    component: propertyValues,
  },
  {
    path: `/property`,
    name: `property`,
    component: property,
  },
  {
    path: `/goodClass`,
    name: `goodClass`,
    component: goodClass,
  },
  {
    path: `/goods`,
    name: `goods`,
    component: goods,
  },
  {
    path: `/about`,
    name: `About`,
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ `../views/About.vue`),
  },
]

const router = new VueRouter({
  mode: `hash`,
  base: process.env.BASE_URL,
  routes,
})
router.beforeEach((to, from, next) => {
  const noAuthPage = [`/login`, `/register`]
  if (noAuthPage.includes(to.path) === false) {
    if (localStorage.getItem(`user`)) {
      next()
    } else {
      next({
        path: `/login`,
      })
    }
  } else {
    next()
  }
})
export default router
